//
//  ViewController.m
//  Commom
//
//  Created by 山刘 on 2018/10/26.
//  Copyright © 2018年 山刘. All rights reserved.
//

#import <CoreFoundation/CoreFoundation.h>
#import "ViewController.h"
#import "CommomHeader.h"
#import "T1ViewController.h"

#import "SEGUrlparamsModel.h"
#import "SEGSystemnativeModel.h"

#import "YYKitMacro.h"
#import "NSString+YYAdd.h"

static inline NSString* serverUrl_login(NSString*context){ return [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-cbs",@"/login.json",nil];}

@interface ViewController ()
@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.navigationController pushViewController:[[SEGUhomeMediator sharedInstance]seg_initPhotoLibraryViewController:nil] animated:YES];
    

//    [[SEGUhomeMediator sharedInstance]seg_BridgeOfLogicCompletedV2:^id _Nonnull(id  _Nonnull result, NSURL * _Nonnull url) {
//
//        [[[UIAlertView alloc]initWithTitle:@"提示" message:url.host delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show];
//
////        [self.navigationController pushViewController:[[SEGUhomeMediator sharedInstance] seg_initPayViewController:@{}] animated:YES];
//
//        NSLog(@"touchesBegan %@",url.absoluteString);
//        if (![url.scheme isEqualToString:@"uhomeoc"]) return nil;
//
//        if ([url.host isEqualToString:@"thirdparty.pay"]){
//            SEGUrlparamsModel * data = (SEGUrlparamsModel*)result;
//            if ([data.payCode isEqualToString:@"WEIXIN"]) {
//                NSLog(@"微信支付接入！");
//            }
//            else if ([data.payCode isEqualToString:@"PENGHUA"]) {
//                NSLog(@"鹏华支付接入！");
//            }
//        }
//
//        if ([url.host isEqualToString:@"thirdparty.goPayList"]){
//            //SEGUrlparamsModel * data = (SEGUrlparamsModel*)result;
//            NSLog(@"四格支付接入！");
//        }
//        if ([url.host isEqualToString:@"system.socialShare"]){
//            //SEGUrlparamsModel * data = (SEGUrlparamsModel*)result;
//            NSLog(@"分享接入！");
//        }
//        if ([url.host isEqualToString:@"system.uploadFiles"]){
//            SEGSystemnativeModel * data = (SEGSystemnativeModel*)result;
//            NSLog(@"文件上传接入！%@",data.fn);
//
//            NSMutableArray <UIImage *>*imageArr = [NSMutableArray array];
//            [data.para.uri enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                UIImage *newImage = [UIImage imageWithContentsOfFile:obj];
//                if (newImage) [imageArr addObject:newImage];
//            }];
//
//            return @{@"code":@"0",@"message":@"message",@"data":@""};
//        }
//        if ([url.host isEqualToString:@"system.jumpToNative"]){
//            SEGSystemnativeModel * data = (SEGSystemnativeModel*)result;
//            NSLog(@"文件上传接入！%@",data.page);
//
//            NSMutableDictionary *params = [NSMutableDictionary dictionary];
//
//            if ([data.page isEqualToString:@"MY_POINT"]) {}
//            if ([data.page isEqualToString:@"BONUSES_ORDER_CONFIRM"]) {}
//            if ([data.page isEqualToString:@"ADVERTISEMENT"]) {
//                [params setValue:data.para.typeId forKey:@"typeId"];
//                [params setValue:data.para.href forKey:@"href"];
//                [params setValue:data.para.title forKey:@"title"];
//            }
//            if ([data.page isEqualToString:@"FIVE_STAR_RATING"]) {}
//            if ([data.page isEqualToString:@"GOOD_STAFF"]) {}
//            if ([data.page isEqualToString:@"CONVENIENCE_SERVICE"]) {}
//            if ([data.page isEqualToString:@"BUSINESS_SERVICE"]) {}
//            if ([data.page isEqualToString:@"ASSETS_AUTHENTICATION"]) {}
//        }
//        if ([url.host isEqualToString:@"homeservice.confirmOrder"]){
//            //SEGUrlparamsModel * data = (SEGUrlparamsModel*)result;
//            NSLog(@"订单页跳转接入！");
//        }
//        if ([url.host isEqualToString:@"homeservice.counseling"]){
//            SEGUrlparamsModel * data = (SEGUrlparamsModel*)result;
//            NSString* goMessage = [NSString stringWithFormat:@"100888|%@|%@|%@|%@", data.providerSid, data.serviceSid, data.type, [SEGProfileManager sharedManager].profile.userId];
//            NSLog(@"客服跳转接入！%@",goMessage);
//        }
//        if ([url.host isEqualToString:@"business.openActDetail"]){
//            NSDictionary * data = (NSDictionary*)result;
//            if (data && [data isKindOfClass:[NSDictionary class]] && [data allKeys].count > 0) {
//            }
//
//            NSLog(@"商品详情接入！");
//        }
//        if ([url.host isEqualToString:@"homeservice.setSharePara"]){
//            //SEGUrlparamsModel * data = (SEGUrlparamsModel*)result;
//            NSLog(@"分享接入！");
//        }
//
//        return nil;
//    }];
    
   
    
    [[NSUserDefaults standardUserDefaults] setObject:UserDefaultsClientServerValue_gama forKey:UserDefaultsClientServerKey];
    [[NSUserDefaults standardUserDefaults] setObject:UserDefaultsBrowerServerValue_gama forKey:UserDefaultsBrowerServerKey];
    [[NSUserDefaults standardUserDefaults] setObject:@"42" forKey:UserDefaultsClientVersionCodeTypeKey];
    
    [SEGHTTPBusinessManager seg_serRequestHeaderDictionary:@{
                                                             @"systemVersionCode":@"11.4.1",
                                                             @"phoneModel":@"iPhone6",
                                                             @"version":@"1.7.0",
                                                             @"SYSCODE":@"10",
                                                             @"communityId":@"67",
                                                             @"source":@"2",
                                                             @"platform":@"CMHK",
                                                             @"deviceId":@"FCACB98B-53CA-4314-AB10-56B6E7D2AA1B",
                                                             @"versionCode":@"41"
                                                             }];
    
//    [[SEGLoginV2Manager sharedManager]requestLoginWithPassword:@"222222" tel:@"18816820401" completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//        [[SEGProfileManager sharedManager]requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//            if (isSuccessed) {
//                [[SEGPaytradeManager sharedManager]requestPaytradeForSubmitOrderPayWithPayMethod:@"3" orderId:@"" payeeId:@"" finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//                }];
//            }
//        }];
//    }];
    
}
-(void)dealloc{
//    [SEGLoginV2Manager destroyResource];
//    [SEGProfileManager destroyResource];
    [SEGRefreshManager destroyResource];
    [SEGFindcommunitybynameManager destroyResource];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:@"http://123.207.94.227:8080/MYWebApplication-war-exploded/LoginServlet.json" parameters:nil completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
//    }];
    
    return;
//    [[SEGUhomeMediator sharedInstance]seg_destroyMemory];
    //    NSString *string = [@"params={\"isTransparent\":\"0\",\"title\":\"aaa,bbb,cccc\",\"urls\":\"aaa,bbb,cccc\",\"selected\":\"1\",\"hasShare\":\"1\",\"fn\":\"1\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://homeservice.setHeadTitle?%@",string];
    
    
    //    NSString *string = [@"params={\"icon\":\"1\",\"title\":\"aaa,bbb,cccc\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://homeservice.setSharePara?%@",string];
    
    //    NSString *string = [@"params={\"providerSid\":\"1\",\"serviceSid\":\"aaa,bbb,cccc\",\"type\":\"1\",\"fn\":\"1\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://homeservice.counseling?%@",string];
    
    //    NSString *string = [@"params={\"providerSid\":\"1\",\"serviceSid\":\"aaa,bbb,cccc\",\"type\":\"1\",\"fn\":\"1\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://homeservice.confirmOrder?%@",string];
    
    //    NSString *string = [@"params={\"page\":\"ADVERTISEMENT\",\"para\":{\"advertisingId\":\"广告id\",\"typeId\":\"跳转类型id\",\"href\":\"跳转路径\",\"title\":\"标题\"}}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://system.jumpToNative?%@",string];
    
    //    NSString *string = [@"params={\"fn\":\"ADVERTISEMENT\",\"para\":{\"number\":\"xxx\",\"editale\":\"xxx\",\"type\":\"2\"}}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://system.camera?%@",string];
    
    //    NSString *string = [@"params={\"fn\":\"fn\",\"para\":{\"serviceType\":\"xxx\",\"uri\":[\"xxx\",\"xxx\"]}}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://system.uploadFiles?%@",string];
    
    
    //    NSString *string = [@"params={\"title\":\"xxx\",\"icon\":\"xxx\",\"content\":\"xxx\",\"url\":\"xxx\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://system.socialShare?%@",string];
    
    //    NSString *string = [@"params={\"url\":\"xxx\",\"id\":\"xxx\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://thirdparty.goPayList?%@",string];
    
    //    NSString *string = [@"params={\"fn\":\"xxx\",\"number\":\"xxx\",\"editable\":\"1\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://thirdparty.takePhoto?%@",string];
    
    //    NSString *string = [@"params={\"fn\":\"funcname\",\"title\":\"标题\",\"content\":\"内容\",\"type\":\"1\"}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://thirdparty.openDialog?%@",string];
    
    //    NSString *string = [@"params={\"payCode\":\"PENGHUA\",\"cbUrl\":\"xxx\",\"payInfo\":{\"payerId\":\"value\",\"vpayId\":\"value\",\"desc\":\"value\",\"addInfo\":\"value\",\"notifyUrl\":\"value\",\"amount\":\"value\",\"pHCallbackUrl\":\"value\",\"communityId\":\"value\",\"businessType\":\"value\"}}" stringByURLEncode];
    //    NSString *url = [NSString stringWithFormat:@"uhomeoc://thirdparty.pay?%@",string];
    
    
    
//    [[SEGLoginV2Manager sharedManager]requestLoginWithPassword:@"222222" tel:@"18816820401" completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//        NSLog(@"%@",[SEGLoginV2Manager sharedManager].login.message);
//
//        [[SEGProfileManager sharedManager]requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//            NSLog(@"%@",[SEGProfileManager sharedManager].profile.message);
//
//        }];
//    }];
//    UIImage *data =  [UIImage imageNamed:@"test"];
//    NSData *imageData = UIImagePNGRepresentation(data);
//    [[SEGHTTPManager sharedNetworkManager]uploadImagesWithURLString:@"http://gamma.uhomecp.com/uhomecp-app/common/oneFileUpload.json" parameters:nil WithDataArray:@[imageData] completed:^(SEGHTTPSessionManagerResponseType responseType, double progressValue, id result, NSError *error) {
//
//    }];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *object in @[@"1",@"2",@"3",@"4"]) {
//        SEGUploadFileModel *data = [SEGUploadFileModel new];
//        data.data = UIImagePNGRepresentation([UIImage imageNamed:@"test"]);
//        data.fileName = [NSString stringWithFormat:@"%@.jpg",object];
//        data.mimeType = SEGMimeTypeJPEG;
//        [arr addObject:data];
        
//        [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"a1" ofType:@"mp3"]];
        [arr addObject:[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"a1" ofType:@"mp3"]]];
    }
    
//    [[SEGHTTPManager sharedNetworkManager]uploadDatasWithURLString:@"http://gamma.uhomecp.com/uhomecp-app/common/multiUpload.json" parameters:nil sources:arr progress:^(double progressValue) {
//        NSLog(@"%f",progressValue);
//    } completed:^(id result, NSError *error) {
//
//    }];
    
    SEGUploadFileModel *d1 = [SEGUploadFileModel new];
    d1.data = UIImagePNGRepresentation([UIImage imageNamed:@"test"]);
    d1.mimeType = SEGMimeTypeJPEG;
    d1.fileName = @"a1.jpg";
    
    SEGUploadFileModel *d2 = [SEGUploadFileModel new];
    d2.data = UIImagePNGRepresentation([UIImage imageNamed:@"test"]);
    d2.mimeType = SEGMimeTypeJPEG;
    d2.fileName = @"a2.jpg";
    
//    [[SEGMultipartfileuploadManager sharedManager]requestMultipartfileuploadWithDetailData:@[d1,d2] progress:^(double progressValue) {
//        NSLog(@"%f",progressValue);
//    } completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//
//    }];
    
    NSData *imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"a1" ofType:@"mp3"]];
//    [[SEGUploadmultipartfileManager sharedManager]requestVoiceWithData:imageData progress:^(double progressValue) {
//        NSLog(@"%f",progressValue);
//    } completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//
//    }];

//    [[SEGUploadmultipartfileManager sharedManager]requestVoicesWithData:@[imageData,imageData] progress:^(double progressValue) {
//        NSLog(@"%f",progressValue);
//    } completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//
//    }];
    
    
//    [[SEGMultipartfileuploadManager sharedManager]requestMultipartfileuploadWithData:@[UIImagePNGRepresentation([UIImage imageNamed:@"test"])] serviceType:@"0" progress:^(double progressValue) {
//        NSLog(@"%f",progressValue);
//    } completed:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
//
//    }];
    
//    CGFloat maxQuality = 1.0;
//    CGFloat minQuality = 0.0;
//    int flag = 0;
//
//    while (1) {
//        CGFloat midQuality = (maxQuality + minQuality)/2.0;
//        maxQuality = midQuality;
//        NSLog(@"%f",midQuality);
//
//    }
    
    CGFloat ex = 210735;//99823
    UIImage *img = [UIImage imageNamed:@"test"];
    
    NSData *a1 = UIImageJPEGRepresentation(img, 1.0);
    CGFloat ex1 = a1.length;
    
    CGFloat res = ex/ex1;
    NSLog(@"res===%f==%lu",res,UIImageJPEGRepresentation(img, res).length);
    NSLog(@"1.0==%lu",UIImageJPEGRepresentation(img, 1.0).length);
    NSLog(@"0.9==%lu",UIImageJPEGRepresentation(img, 0.9).length);
    NSLog(@"0.8==%lu",UIImageJPEGRepresentation(img, 0.8).length);
    NSLog(@"0.7==%lu",UIImageJPEGRepresentation(img, 0.7).length);
    NSLog(@"0.6==%lu",UIImageJPEGRepresentation(img, 0.6).length);
    NSLog(@"0.5==%lu",UIImageJPEGRepresentation(img, 0.5).length);
    NSLog(@"0.4==%lu",UIImageJPEGRepresentation(img, 0.4).length);
    NSLog(@"0.3==%lu",UIImageJPEGRepresentation(img, 0.3).length);
    NSLog(@"0.2==%lu",UIImageJPEGRepresentation(img, 0.2).length);
    NSLog(@"0.1==%lu",UIImageJPEGRepresentation(img, 0.1).length);
//    NSLog(@"adjustImageWithinLenght==%lu",[[img adjustImageWithinLenght:ex] length]);

    //imageByRoundCornerRadius
//    NSData *img2 = [self imageCompressToData:img];
    
    [self.navigationController pushViewController:[T1ViewController new] animated:YES];
}


- (NSData *)imageCompressToData:(UIImage *)image{
    NSData *data=UIImageJPEGRepresentation(image, 1.0);
    if (data.length>300*1024) {
        if (data.length>1024*1024) {//1M以及以上
            data=UIImageJPEGRepresentation(image, 0.1);
        }else if (data.length>512*1024) {//0.5M-1M
            data=UIImageJPEGRepresentation(image, 0.5);
        }else if (data.length>300*1024) {//0.25M-0.5M
            data=UIImageJPEGRepresentation(image, 0.9);
        }
    }
    return data;
}
@end
